$(document).ready(function(){
    $('#game_area').change(function(){
        var area_id = $(this).val();
        if(area_id != '0'){
            get_servers_by_area(area_id);
        } else {
            set_servers_default();
        }
    });
    $('#game_id').change(function(){
        var game_id = $(this).val();
        if(game_id != '0'){
            update_group_span(game_id);
        }
    });
    $('#reg_form').submit(function(){
        return check_register();
    });
    $('#pwd_form').submit(function(){
        return check_form_value();
    });
})

//检查注册项
function check_register() {
    if(!check_reg_name() || !check_reg_pwd() || !check_reg_bank() || !check_reg_tel() || !check_reg_qq() || !check_reg_email()){
        return false;
    }
    return true;
}

//检查注册用户名
function check_reg_name() {
    var realname = $('#realname').val();
    if(realname.length == 0){
        set_invalid_item('realname_span','姓名不能为空！');
        invalid_input('realname');
        return false;
    } else if(realname.length < 2 || realname.length > 10) {
        set_invalid_item('realname_span','姓名长度不符合要求，长度为2-5个汉字');
        invalid_input('realname');
        return false;
    }
    clear_html('realname_span');
    clear_css('realname');
    return true;
}

//检查注册密码
function check_reg_pwd(){
    var pwd1 = $('#password').val();
    var pwd2 = $('#password2').val();
    if(pwd1.length == 0){
        set_invalid_item('password_span','密码不能为空！');
        invalid_input('password');
        return false;
    } else if(pwd1.length < 7 || pwd1.length > 16){
        set_invalid_item('password_span','密码长度不符合要求！长度7-18位');
        invalid_input('password');
        return false;
    }
    clear_html('password_span');
    clear_css('password');
    if(pwd2.length == 0){
        set_invalid_item('password2_span','密码不能为空！');
        invalid_input('password2');
        return false;
    } else if(pwd2.length < 7 || pwd2.length > 16){
        set_invalid_item('password2_span','密码长度不符合要求！长度7-18位');
        invalid_input('password2');
        return false;
    }
    if (pwd1 != pwd2) {
        set_invalid_item('password2_span','两次密码输入不一致，请重新输入！');
        invalid_input('password2');
        return false;
    }
    clear_html('password2_span');
    clear_css('password2');
    return true;
}

//检查银行帐号
function check_reg_bank(){
    var bank1 = $('#bank_account').val();
    var bank2 = $('#bank_account2').val();
    if(bank1.length == 0){
        set_invalid_item('bank_account_span','银行帐号不能为空！');
        invalid_input('bank_account');
        return false;
    } else if(bank1.length < 12){
        set_invalid_item('bank_account_span','银行帐号长度不符合要求！');
        invalid_input('bank_account');
        return false;
    }
    if(bank2.length == 0){
        set_invalid_item('bank_account2_span','银行帐号不能为空！');
        invalid_input('bank_account2');
        return false;
    } else if(bank2.length < 12){
        set_invalid_item('bank_account2_span','银行帐号长度不符合要求！');
        invalid_input('bank_account2');
        return false;
    }
    if (bank1 != bank2) {
        set_invalid_item('bank_account2_span','两次银行帐号输入不一致，请重新输入！');
        invalid_input('bank_account2');
        return false;
    }
    clear_html('bank_account_span');
    clear_html('bank_account2_span');
    clear_css('bank_account');
    clear_css('bank_account2');
    return true;
}

//检查手机和电话
function check_reg_tel(){
    var mobile  = $('#mobile').val();
    var tel     = $('#tel').val();
    if(mobile.length == 0 && tel.length == 0){
        set_invalid_item('tel_span','手机号和电话请至少填写一项！');
        invalid_input('tel');
        return false;
    }
    clear_html('tel_span');
    clear_css('tel');
    return true;
}

//检查QQ号
function check_reg_qq(){
    var qq = $('#qq').val();
    if(qq.length < 4){
        set_invalid_item('qq_span','QQ号长度不正确！');
        invalid_input('qq');
        return false;
    }
    clear_html('qq_span');
    clear_css('qq');
    return true;
}

//检查email
function check_reg_email(){
    var email = $('#email').val();
    if(!checkEmail(email)){
        set_invalid_item('email_span','邮箱格式不符合要求，请重新填写！');
        invalid_input('email');
        return false;
    }
    clear_html('email_span');
    clear_css('email');
    return true;
}

function invalid_input(obj_id){
    set_css(obj_id,'red_border');
    set_focus(obj_id);
}

function set_invalid_item(obj_id,desc){
    set_html(obj_id,desc);
    set_css(obj_id,'red_font');
}

function set_html(obj_id,desc){
    $('#'+obj_id).html(desc);
}

function set_css(obj_id,class_name){
    $('#'+obj_id).attr('class',class_name);
}

function clear_html(obj_id){
    $('#'+obj_id).html('');
}

function clear_css(obj_id){
    $('#'+obj_id).attr('class','');
}

function set_focus(obj_id){
    $('#'+obj_id).focus();
}

/**
 *
 * 检查项目
 *
 */
function check_item_empty(item_id,desc) {
    var obj = '#'+item_id;
    var tip = '#'+item_id+'_span';
    var val = $(obj).val();
    if (!val) {
        $(tip).html(desc);
        $(tip).attr('class','red_font');
        $(obj).attr('class','red_border');
        $(obj).focus();
        return false;
    } else {
        $(tip).html('');
        //$(tip).html(' * 符合要求');
        //$(tip).attr('class','green_font');
        $(obj).attr('class','');
        return true;
    }
}

function update_group_span(game_id){
    //
}

function get_servers_by_area(area_id){
    var a = new Ajax('/ajax/servers/'+area_id+'/','',change_server_select,'GET');
    a.load();
}

function change_server_select(txt){
    $('#server_span').html(txt);
}

function set_servers_default(){
    var txt = '<select name="game_server" id="game_server"><option value="0">请选择</option></select>';
    change_server_select(txt);
}

function show_desc(fid,sid){
    var obj_id = '#pdesc'+fid+sid;
    $('div.pdesc').not(obj_id).css('display','none');
    $(obj_id).slideToggle();
}

function check_form_value(){
    var user = $('#user').val();
    var email = $('#email').val();
    if(user.length == 0){
        $('#tip').html('必须填写用户姓名或ID才能继续');
        $('#tip').attr('class','red_font');
        return false;
    } else {
        $('#tip').html('');
    }
    if(email.length == 0){
        $('#tip').html('必须填写注册邮箱方可继续');
        $('#tip').attr('class','red_font');
        return false;
    } else if(!checkEmail(email)){
        $('#tip').html('邮箱格式不符合要求！');
        $('#tip').attr('class','red_font');
        return false;
    } else {
        $('#tip').html('');
    }
    return true;
}


function check_order_search(){
	var w = $('#word').val();
	if(w.length == 0){
		alert('你输入要搜索的内容！');
		$('#word').focus();
		return false;
	} else if (w.length > 20){
		alert('搜索内容过长，请重新输入');
		$('#word').focus();
		$('#word').select();
		return false;
	}
	return true;
}

function check_order() {
    var data = $('#order_form').serialize();
    var temp = data.split('&');
    var params = new Array();
    for (var i=0; i<temp.length; i++) {
        var arr = temp[i].split('=');
        params[arr[0]] = arr[1];
    }
    if (!params['protocol']) {
        alert('你必须同意玩家协议方可继续！');
        return false;
    }
    if (params['game_area'] == '0') {
        alert('请选择游戏区域！');
        return false;
    } else if (params['game_server'] == '0') {
        alert('请选择游戏服务器！');
        return false;
    } else if (params['role_group'] == '0') {
        alert('请选择角色阵营！');
        return false;
    } else if (params['role_career'] == '0') {
        alert('请选择角色职业！');
        return false;
    } else if (!params['role_name']) {
        alert('请输入角色名！');
        return false;
    } else if (!params['role_lvl']) {
        alert('请输入角色目前等级！');
        return false;
    } else if (!params['tel']) {
        alert('请正确填写电话或手机信息');
        return false;
    } else if (!params['qq']) {
        alert('为方便联系，请填写正确的QQ号码');
        return false;
    }
    return true;
}

function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

function SetHome(obj,vrl){
    try{
        document.body.style.behavior = 'url(#default#homepage)';
　　　　document.body.setHomePage(vrl);
    }
    catch(e){
        if(window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage',vrl);
        }
    }
}


function checkEmail(strEmail) {
    var myReg = /^[a-z0-9]+[_\.]*[a-z0-9]*@[a-z0-9]+(\.[a-z0-9]{2,3})+$/;
    if(myReg.test(strEmail)) return true;
    return false;
}